<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Sentiment
 *
 * @author QuangKhoi
 */
class Sentiment {

    public $id;
    public $userId;
    public $messageId;
    public $threadId;
    public $score;
    public $dateCreated;
    public $streamId;
    public $type;
    public $source;

    public function __construct($userId, $messageId, $threadId, $score, $date, $streamId, $type, $source, $id = "") {
        $this->id = $id;
        $this->userId = $userId;
        $this->messageId = $messageId;
        $this->threadId = $threadId;
        $this->score = $score;
        $this->streamId = $streamId;
        $this->dateCreated = $date;
        $this->type = $type;
        $this->source = $source;
        try {
            $this->dateCreated = new DateTime($date);
        } catch (Exception $err) {
            
        }
    }

    public static function getScore($messageId, $streamId) {
        $userId = User::getCurrentUserId();
        $whereType = '';
        $user = User::getLogedInUser();
        $query = "SELECT * FROM sentimentdata WHERE (userid='$userId' OR userid IN (SELECT userid FROM users WHERE teamid=$user->teamId)) AND messageid='$messageId' AND streamid = '$streamId'";
        if ($user->type == 10) //demo user
            $query = "SELECT * FROM sentimentdata WHERE messageid='$messageId' AND streamid = '$streamId'";
        $result = Database::getInstance()->query($query);
        if ($result->dataTable->numOfRow == 0) {
            $query = "SELECT * FROM sentimentdata WHERE userid='0' AND messageid='$messageId' AND streamid = '$streamId'";
            if ($user->type == 10) //demo user
                $query = "SELECT * FROM sentimentdata WHERE messageid='$messageId' AND streamid = '$streamId'";
            $result = Database::getInstance()->query($query);
            if ($result->dataTable->numOfRow == 1) {
                $row = $result->dataTable->rows[0];
                return $row["score"];
            } else {
                return -2;
            }
        } else {
            $row = $result->dataTable->rows[0];
            return $row["score"];
        }
    }

    public static function retrieveScore($messageId, $streamId) {
        $userId = User::getCurrentUserId();
        $whereType = '';
        $user = User::getLogedInUser();
        $query = "SELECT * FROM sentimentdata WHERE  (userid='$userId' OR userid IN (SELECT userid FROM users WHERE teamid=$user->teamId))  AND messageid='$messageId' AND streamid = '$streamId'";
        $result = Database::getInstance()->query($query);
        if ($result->dataTable->numOfRow == 0) {
            $query = "SELECT * FROM sentimentdata WHERE userid='0' AND messageid='$messageId' AND streamid = '$streamId'";
            $result = Database::getInstance()->query($query);
            if ($result->dataTable->numOfRow == 1) {
                $row = $result->dataTable->rows[0];
                return array('score' => $row["score"], 'user' => 0);
            } else {
                return -2;
            }
        } else {
            $row = $result->dataTable->rows[0];
            return array('score' => $row["score"], 'user' => $row['userid']);
        }
    }

    public function save() {
        $model = $this;
        $user = User::getLogedInUser();
        $userScoring = User::getSwitchFrom();
        $query = "SELECT * FROM sentimentdata WHERE  (userid='$model->userId' OR userid IN (SELECT userid FROM users WHERE teamid=$user->teamId))  AND messageid='$model->messageId' AND threadid='$model->threadId' AND streamid='$model->streamId'";
        $model->id = Database::getID('sentimentdata', 'id');
        $result = Database::getInstance()->query($query);
        if ($result->dataTable->numOfRow == 0) {
            $date = $this->dateCreated->format("Y-m-d H:i:s");
            $query = "INSERT INTO sentimentdata 
                                    (userid, messageid, threadid, score, datecreated, streamid, type, source, userscoring) VALUES
                                    ('$model->userId',
                                    '$model->messageId', '$model->threadId', '$model->score', '$date', '$model->streamId', '$model->type', '$model->source', '$userScoring')";
            Database::getInstance()->query($query);
            return $model;
        } else {
            $query = "UPDATE sentimentdata SET score = '$model->score', edited = '$userScoring' WHERE userid='$model->userId' AND messageid='$model->messageId' AND streamid='$model->streamId'";
            Database::getInstance()->query($query);
            return $model;
        }
    }

    public static function saveSentiment($text, $streamId, $messageId, $threadId, $date, $type, $source) {
        $sentimentScore = Sentiment::getScore($messageId, $streamId);
        if ($sentimentScore !== -2) {
            $sentimentText = ($sentimentScore == 0) ? "neutral" : ($sentimentScore > 0 ? "positive ..." : ($sentimentScore == -1 ? "negative ..." : "irrelevant"));
            $sentimentColor = ($sentimentScore == 0) ? "#9bbb59" : ($sentimentScore > 0 ? "#4f81bd" : ($sentimentScore == -1 ? "#c0504d" : "#ffe563"));
            return array("score" => $sentimentScore,
                "text" => $sentimentText,
                "color" => $sentimentColor);
        } else {
            $sentimentScore = Utils::detectSentiment($text);
            $color = '';
            //if ($streamId == 119 || $streamId == 162 || $streamId == 163 || $streamId == 167 || $streamId == 168 || $streamId == 169 || $streamId == 177 || $streamId == 178) {
                $sentimentScore = 0;
                return array("score" => $sentimentScore,
                "text" => 'undefined',
                "color" => '#EEE');
            /*}
            $sentimentData = new Sentiment(0, $messageId, $threadId, $sentimentScore, $date, $streamId, $type, $source);
            $sentimentData->save();
            $sentimentScore = Sentiment::getScore($messageId, $streamId);
            $sentimentText = ($sentimentScore == 0) ? "neutral" : ($sentimentScore > 0 ? "positive" : "negative");
            $sentimentColor = ($sentimentScore == 0) ? "#9bbb59" : ($sentimentScore > 0 ? "#4f81bd" : "#c0504d");
            return array("score" => $sentimentScore,
                "text" => $sentimentText,
                "color" => $sentimentColor);*/
        }
    }

    //put your code here
}

?>
